package main

import (
	"encoding/json"
	"fmt"
	"github.com/go-resty/resty/v2"
	"github.com/gogap/logs"
	"io/ioutil"
	"log"
	gconf2 "yundian/cmd/tools/grawl/gconf"
	"yundian/internel/app/models/placemodel/places"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

type DeviceTypeModelGenerated struct {
	Code int `json:"code"`
	Body struct {
		Total int `json:"total"`
		Data  []struct {
			ID           int    `json:"placeId"`
			DeviceTypeId int    `json:"deviceTypeId"`
			SharedMode   string `json:"sharedMode"`
			FeeModeName  string `json:"feeModeName"`
			FeeMode      int    `json:"feeMode"`
		} `json:"data"`
	} `json:"body"`
}

func main() {
	dingo.InitViper("./configs/admin.yaml")
	dbs.NewMysqlComponent().Step()

	url := "http://mng.cnman.cn/mng/10082/place/deviceType/list.data"
	urlFeeMode1 := "http://mng.cnman.cn/mng/10082/dynamic/pcl/2/place/virtualProduct.html" // ?deviceTypeId=13&placeId=2290
	urlFeeMode2 := "http://mng.cnman.cn/mng/10082/dynamic/pcl/1/place/virtualProduct/list.data"
	doRequestPrice(url, urlFeeMode1, urlFeeMode2, "/Volumes/zhang/dev-zhongman/记录/任务/中曼迁移数据/价格")
}

func doRequestPrice(url, urlFeeMode1, urlFeeMode2, path string) {
	placesUpdateList := make([]*placemodel.PlaceModel, 0)
	if db := global.Orm.Debug().Table("places p").Where("user_id>? AND id>?", 400000, 30000).Find(&placesUpdateList); db.Error != nil {
		logs.Error(db.Error)
	}

	client := resty.New()
	for _, item := range placesUpdateList {
		m := map[string]string{}
		m["page"] = "1"
		m["limit"] = "30"
		m["placeId"] = fmt.Sprintf("%d", item.Id-30000)
		resp, err := client.R().SetFormData(m).SetHeader("Content-Type", "application/x-www-form-urlencoded").SetHeader("Cookie", gconf2.Cookie).Post(url)
		if err != nil {
			log.Println("抓取失败")
		}

		var targetDeviceTypeModelGenerated DeviceTypeModelGenerated
		json.Unmarshal([]byte(string(resp.Body())), &targetDeviceTypeModelGenerated)

		for _, item1 := range targetDeviceTypeModelGenerated.Body.Data {
			log.Println(item1)
			if item1.SharedMode != "pcl" {
				continue
			}
			if item1.FeeModeName == "套餐模式" {
				m1 := map[string]string{}
				m1["page"] = "1"
				m1["limit"] = "30"
				m1["placeId"] = fmt.Sprintf("%d", item.Id-30000)
				m1["deviceTypeId"] = fmt.Sprintf("%d", item1.DeviceTypeId)
				resp1, err := client.R().SetFormData(m1).SetHeader("Content-Type", "application/x-www-form-urlencoded").
					SetHeader("Cookie", gconf2.Cookie).Post(urlFeeMode2)
				if err != nil {
					log.Println("抓取失败")
				}
				//var targetDeviceTypeModelGenerated DeviceTypeModelGenerated
				//json.Unmarshal([]byte(string(resp1.Body())), &targetDeviceTypeModelGenerated)

				if err = ioutil.WriteFile(fmt.Sprintf(path+"/%d.json", item.Id), resp1.Body(), 666); err != nil {
					log.Println(err.Error())
				}
			} else if item1.FeeModeName == "计时模式" {
				resp2, err := client.R().SetHeader("Cookie", gconf2.Cookie).Get(urlFeeMode1 + fmt.Sprintf("?deviceTypeId=%d&placeId=%d", item1.DeviceTypeId, item1.ID))
				if err != nil {
					log.Println("抓取失败")
				}
				if err = ioutil.WriteFile(fmt.Sprintf(path+"/%d.html", item.Id), resp2.Body(), 666); err != nil {
					log.Println(err.Error())
				}
			}
		}
	}
}
